<?php
class MaestroDetalleHelper extends AppHelper {
    private $tags = array(
        'javascriptstart' => '<script type="text/javascript">',
        'javascriptend' => '</script>',
	'javascriptblock' => '<script type="text/javascript">%s</script>',
	'javascriptlink' => '<script type="text/javascript" src="%s"></script>'
    );
    private $helpers = array('Html','Form','Time','Number','Paginator','Session');
    private $_html =array('color'=>null,'width'=>null, 'height'=>null, 'style'=>null,'align'=>null,'border'=>null);
    private $_dato;
    private $_key;
	
    function getProperty($pro=array()){
            $property=' ';
            foreach($pro as $name => $value){
                if(!is_array($name)){
                    $property .= $name.'="'.$value.'" ';
                }
            }
            return $property;
    }
	
    function getValue($dato,$key,$trong=false){
            $val = str_replace('[','',$key);
            $dat = explode(']',$val);
            $valor = $dato;
            foreach($dat as $d){
                if($d != false){
                    $valor = isset($valor[$d])?$valor[$d]:false;
                }
            }
            $valor = $this->transform($valor);

            if ($trong)
                return sprintf($this->tags['strong'], $valor);
            else
                return trim($valor);
    }
	
    function getButton($f){
            $form 	= $this->Form;
            $boton ='';
            $boton.='<a href="'.Router::url('../').'" class="lnkbutton btnback">Volver</a>';
            $boton.='<a href="'.Router::url('add').'" class="lnkbutton btnnew">Nuevo</a>';
            $boton.= $form->submit('Eliminar', array(
                    'div' => false,
                    'value'=>true,
                    'name'=>'delete',
                    'class'=>'btnerase'
            ));

            return $boton;
    }
	
    private function formatHtml($arg = array()){
            //$_temp = array_intersect_assoc($this->_html, $arg);
            $array = array();
            foreach($this->_html as $key=>$value){
                    if(isset($arg[$key]))
                            $array[$key] = $arg[$key];
            }
            return $this->getProperty($array);
    }
	
     private function formatHtml($arg = array()){
        //$_temp = array_intersect_assoc($this->_html, $arg);
        $array = array();
        foreach($this->_html as $key=>$value){
                if(isset($arg[$key]))
                        $array[$key] = $arg[$key];
        }
        return $this->getProperty($array);
    }
	
//    input: identificador del objeto DOM (input) - obligatorio
	/*
	call
	$MaestroDetalle->buildDetalle(
		array(
			'id'=>'DocumentoDetalle', 
			'action'=>'',
			'value' =>1,
			'params'=>array(),
			'events'=>array(	
				'onAdd'=>null,
				'onEdit'=>null,
				'onDelete'=>null,
				'onDelete'=>null,
			),
			'fields' =>array(
				array(
					'name'=>'cantidad',					
					'options'=>array(
						'type'=>'text',
						'id'=>'cantidad',
						'onchange'=>'alert(this.value)',
						'visible'=>true,
					)
				),
				array(
					array(
						'name'=>'producto_id',					
						'options'=>array(
							'type'=>'hidden',
							'visible'=>false,
							'id'=>'producto_codigo',
							'size'=>'5',
							'onchange'=>'text',
					),
					array(
						'name'=>'producto_codigo',
						'options'=>array(
							'type'=>'text',
							'visible'=>true,
							'id'=>'producto_codigo',
							'size'=>'5',
							'onchange'=>'text',
					),
					array(
						'name'=>'producto_descripcion',
						'options'=>array(
							'type'=>'text',
							'id'=>'producto_codigo',
							'id'=>'producto_codigo',
							'visible'=>true,
							'size'=>'20',
							'onchange'=>'this.value.UpperCase()',
					)
				
				),
			))
		)
	*/
	
    public function buildDetalle($args = array()){		
            $detail = '<table><tr>';
            foreach($args['fields'] as $field){
                    $detail .= '<td '.$this->formatHtml($field).'>'.'???'.'</td>';

            }
    }
    public function script(){
            $script .=	'$("#addBoton").click(function(){ \\
              var thtml;
              var div;

              if($("#DescripcionDetalle").val() != ""){
                      numDetalle++;
                      puntero = numDetalle;

                      var Prefix = "data[DocumentoCompra][DocumentoCompraDetalle]["+numDetalle+"]";

                      thtml="<table><tr><td width="49" id=\'tdCantidad\'+numDetalle+">";
                      thtml= thtml+ $("#CantidadDetalle").val();
                      thtml= thtml+"</td>";
                      thtml= thtml+\'</td><td width="45" align="center">\';
                      thtml= thtml+\'<input type="button" id="editar\'+numDetalle+\'" class="editarDetalle" value="E" style="width:10px">&nbsp;\';
                      thtml= thtml+\'<input type="button" id="delete'+numDetalle+'" class="deleteDetalle" value="D" style="width:10px">&nbsp;\';
                      thtml= thtml+\'<input type="hidden" id="\'+Prefix+\'[producto_id]" name="\'+Prefix+\'[producto_id]">\';
                      thtml= thtml+\'<input type="hidden" id="\'+Prefix+\'[eliminado]" name="\'+Prefix+\'[eliminado]" value="0">\';
                      thtml= thtml+\'</td></tr></table>\';

                      div = document.createElement(\'div\');
                      div.id=\'detalle\'+numDetalle;
                      div.style["position"]=\'relative\';
                      div.style["width"]=\'99%\';

                      document.getElementById(\'Detalle\').appendChild(div);
                      $("#detalle"+numDetalle).html(thtml);
                      document.getElementById(\'detalle\'+numDetalle).style["margin"]="-6px";

                      document.getElementById(Prefix+"[producto_id]").value = $("#ProductoIdDetalle").val();
                      document.getElementById(Prefix+"[codigo]").value = $("#CodigoDetalle").val();
                      document.getElementById(Prefix+"[descripcion_producto]").value = $("#DescripcionDetalle").val();
                      document.getElementById(Prefix+"[cantidad]").value = $("#CantidadDetalle").val();
                      document.getElementById(Prefix+"[valor_venta_unidad]").value = $("#ValorUnitarioDetalle").val();
                      document.getElementById(Prefix+"[valor_venta_total]").value = $("#ValorTotalDetalle").val();
                      document.getElementById(Prefix+"[impuesto]").value = $("#ImpuestosDetalle").val();
                      document.getElementById(Prefix+"[importe_total]").value = $("#ImporteTotalDetalle").val();
                      document.getElementById(Prefix+"[ref_percepcion]").value = $("#RefPercepcionDetalle").val();
                      document.getElementById(Prefix+"[ref_impuesto]").value = $("#RefImpuestoDetalle").val();
                      document.getElementById(Prefix+"[percepcion]").value = $("#PercepcionDetalle").val();

                      $("#ProductoIdDetalle").val(\'\');
                      $("#CantidadDetalle").val(\'\');
                      $("#CodigoDetalle").val(\'\');
                      $("#DescripcionDetalle").val(\'\');
                      $("#ValorUnitarioDetalle").val(\'0\');
                      $("#ValorTotalDetalle").val(\'0\');
                      $("#ImpuestosDetalle").val(\'0\');
                      $("#RefPercepcionDetalle").val(\'0\');
                      $("#PercepcionDetalle").val(\'0\');
                      $("#ImporteTotalDetalle").val(\'\');

                      calcularTotales();
                    } else {
                            alert("Ingrese una desripcion")
                    }
                    $("#CantidadDetalle").focus();
            });';
    }

}
?>